﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;
using Google.GData.Client;

namespace GravatarContacts
{
    public partial class GravatarImporterGui : Form
    {
        public delegate void Func();

        GravatarImporter _importer;

        public GravatarImporterGui()
        {
            InitializeComponent();
            progressBar1.Minimum = 1;
            progressBar1.Step = 1;
            progressBar1.Value = 1;
            _importer = new GravatarImporter();
            _importer.AddedEmail += new GravatarImporter.HandleEmail(importer_AddedEmail);
            _importer.FoundGravatar += new GravatarImporter.HandleEmail(importer_FoundGravatar);
            _importer.ContactsLoaded += new GravatarImporter.HandleContactsCount(importer_ContactsLoaded);
            _importer.Finished += new GravatarImporter.HandleFinish(_importer_Finished);
        }

        void _importer_Finished(int totalContactsCount, int totalContactsWithPhotosCount, int gravatarsCount)
        {
            Func del = delegate
                {
                    listBox1.Items.Add("Total contacts processed: " + totalContactsCount.ToString());
                    listBox1.Items.Add("Total contacts with photos: " + totalContactsWithPhotosCount.ToString());
                    listBox1.Items.Add("Total gravatars added: " + gravatarsCount.ToString());
                    listBox1.Items.Add("Finished.");
                };
            listBox1.Invoke(del);
        }

        private void importButton_Click(object sender, EventArgs e)
        {
            progressBar1.Value = 1;
            listBox1.Items.Clear();
            try
            {
                _importer.Import(usernameTextbox.Text, passwordTextbox.Text);
            }
            catch (InvalidCredentialsException ex)
            {
                MessageBox.Show("The username or password that you've used are invalid. Please correct and try again.");
            }
            catch (Exception ex)
            {
                MessageBox.Show("Something unpredicatable happened. Try doing the rain dance and hit Import again.");
            }
        }

        void importer_FoundGravatar(string email)
        {
            Func del = delegate { listBox1.Items.Add(email); };
            listBox1.Invoke(del);
        }

        void importer_ContactsLoaded(int count)
        {
            Func del = delegate { progressBar1.Maximum = count; };
            progressBar1.Invoke(del);
        }

        void importer_AddedEmail(string email)
        {
            Func del = delegate { progressBar1.PerformStep(); };
            progressBar1.Invoke(del);
        }

        private void buttonAbort_Click(object sender, EventArgs e)
        {
            _importer.Abort();
            progressBar1.Value = 1;
            listBox1.Items.Add("Aborted.");
        }

    }
}